<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>

    <script>
        // 使用 __proto__
        var obj = {};
        var descriptor = Object.create(null); // 没有继承的属性
        // 默认没有 enumerable，没有 configurable，没有 writable
        descriptor.value = 'static';
        Object.defineProperty(obj, 'key', descriptor);

        // 显式
        Object.defineProperty(obj, "key", {
            enumerable: false,
            configurable: false,
            writable: false,
            value: "static"
        });

        // 循环使用同一对象
        function withValue(value) {
            var d = withValue.d || (
                withValue.d = {
                    enumerable: false,
                    writable: false,
                    configurable: false,
                    value: null
                }
            );
            d.value = value;
            return d;
        }
        // ... 并且 ...
        Object.defineProperty(obj, "key", withValue("static"));

        // 如果 freeze 可用, 防止代码添加或删除对象原型的属性
        // （value, get, set, enumerable, writable, configurable）
        (Object.freeze || Object)(Object.prototype);

        /*
        +====================================================================================================+
        +                                        案 例                                                       +
        +====================================================================================================+
        */
        var o = {}; // Creates a new object

        Object.defineProperty(o, 'a', {
            value: 37,
            writable: false
        });

        console.log(o.a); // logs 37
        o.a = 25; // No error thrown
        // (it would throw in strict mode,
        // even if the value had been the same)
        console.log(o.a); // logs 37. The assignment didn't work.

        // strict mode
        (function() {
            'use strict';
            var o = {};
            Object.defineProperty(o, 'b', {
                value: 2,
                writable: false
            });
            o.b = 3; // throws TypeError: "b" is read-only
            return o.b; // returns 2 without the line above
        }());
    </script>
</body>

</html>